package socdev.mobile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.json.JSONObject;

import android.accounts.AccountsException;
import android.content.Context;
import android.util.Log;

public class JSonDownloader {
	 public String download(String path, Context context) {
			StringBuilder builder = new StringBuilder();
			
			
			
			
			HttpClient client = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(path);
			try {
				//HttpContext httpContext = AuthenticatedAppEngineContext.newInstance(context, path);
				HttpResponse response = client.execute(httpGet);
				StatusLine statusLine = response.getStatusLine();
				int statusCode = statusLine.getStatusCode();
				if (statusCode == 200) {
					HttpEntity entity = response.getEntity();
					InputStream content = entity.getContent();
					BufferedReader reader = new BufferedReader(	new InputStreamReader(content));
					String line;
					while ((line = reader.readLine()) != null) {
						builder.append(line);
					}
				} else {
					Log.e(JSonDownloader.class.toString(), "Failed to download file");
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch(Exception e) {
				e.printStackTrace();
			}
			return builder.toString();
		}
	 
	 
	 public static void push(JSONObject object, String path) throws Exception {
		 DefaultHttpClient httpclient = new DefaultHttpClient();
		 HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit
		 HttpPost httpost = new HttpPost(path);
		 System.out.println(object.toString());
		// StringEntity se = new StringEntity("JSON: " + object.toString());
		 //se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
		 
		 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		 
		// object.getString("name")
	     nameValuePairs.add(new BasicNameValuePair("name",object.getString("name")));
	     nameValuePairs.add(new BasicNameValuePair("text",object.getString("text")));
	     httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		 
	//	 httpost.setEntity(se);

		 HttpResponse response = httpclient.execute(httpost);
		 if(response!=null){ 
			 InputStream in = response.getEntity().getContent(); //Get the data in the entity
			 System.out.println(response.getStatusLine().getStatusCode());
			 System.out.println(httpost.toString());
		 }
	}
}
